/*
+--------------------------------------------------------------------------
|   jQuery CDA plugin - Cross-Domain-Ajax made easy
|   
|   CDA, Cross-Domain-Ajax is a library that is able to load data in runtime, similiar to ajax, but from a remote server
|   you can work in passive or active mode:
|   1)  Passive mode - load JS code, that has executing routines of it's own - so it will start as soon as it is loaded.
|   2)  Active Mode: if you do not load code but data (json,xml,arrays) into an aplication, 
|       you have to "wait" until the data is loaded or your app will fail.
|       In Active Mode, a helper function takes care of calling your callback functions just in time and not too early..    
|   
|   CDA works with latest jQuery - tested up to v1.7.2
|   
|   I hope this code is usefull..
|   
|   For usage example, look in the attached file test.html or visit:
|   http://simpeligent.ch/examples/cross-domain-ajax-cda/
|   
|   ========================================
|   jquery.cda.js Copyright (c) 2012 by Hensel Hartmann
|   http://www.simpeligent.ch
|   ========================================
|   simTaco is Dual licensed under the MIT and GPL licenses:
|   http://www.opensource.org/licenses/mit-license.php
|   http://www.gnu.org/licenses/gpl.html
|    
|   File: jquery.cda.js
|   main class
+--------------------------------------------------------------------------
*/

(function($){
	var version = '0.4';
	var options = {};
    var logCount = 0;

  $.cda = function(arguments ) 
  { 
            //Settings list and the default values
            var defaults = {
                debug: 0,
                urid:''
            };
            var method = 'get';
            options = $.extend(defaults, arguments);

                // log debug info
                methods.log('[CDA] Plugin Version: '+version);
                methods.log('[CDA] remote url: '+options.url);
                methods.log('[CDA] unique request id (urid): '+options.urid);
                methods.log('[CDA] options '+options);
                window.console.log(options)

		// Method calling logic
		if ( methods[method] )
		{
			var r = methods[ method ].apply( this);
			return r;
		} else {
			$.error( 'core: Method ' +  method + ' does not exist on jQuery.CDA' );
		}    

	};


    var methods = 
	{
        get: function() 
        {
                // log debug info
                methods.log('[CDA] get: '+options.url);
            // load the data
        	var head = document.getElementsByTagName('head').item(0)
        	script = document.createElement('script');
        	script.src = options.url;
        	script.type = 'text/javascript';
        	script.id = options.urid;
        	head.appendChild(script)
            
            // run the helper-callback function - if in use
            methods.run_callback();
            // return the options to the object so the urid can be used for later work
            return options;
        },
		run_callback : function() 
        {
            if(typeof options.waiter_callback == 'function')
    		{
                    // log debug info
	            	methods.log('run_callback: '+options.waiter_callback);
               // run the helper-callback function
	           options.waiter_callback.call(this,options);        
    		}
        },
        log : function ( content ) 
        { 
            // log debug info
            if (!options.debug ) return;
            $('<div>[cda] '+content+'</div>').appendTo('#debug');
            if (!options.debug || !window.console || !window.console.log) return;
            logCount++;
            if (logCount==1)
            {
                window.console.log('[cda] '+logCount+': '+ 'Plugin Version: ' + version);
                logCount++;
            }
            window.console.log('[cda] '+logCount+': '+ [].join.call(arguments,''));
        }
    }
})(jQuery);

